/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vale.controllers;

import java.util.ArrayList;
import java.util.List;
import org.vale.dao.AbstractFacade;

/**
 *
 * @author Gabriel Bran gbran.u@gmail.com
 * @param <Entity>
 */
public abstract class Catalog<Entity> extends AbstractController {

    private Entity current;
    public static final String ACTION_CREATE = "C";
    public static final String ACTION_UPDATE = "U";
    private String action = Catalog.ACTION_CREATE;
    private List<Entity> entityList = new ArrayList<Entity>();
    
    public void save() {
        try {
            validate();
            if ((action != null) && (action.equalsIgnoreCase(Catalog.ACTION_CREATE))) {
                getAbstractFacade().save(current);
                setAction(Catalog.ACTION_UPDATE);
            } else {
                getAbstractFacade().update(current);
            }
            addInformationMessage("Data saved.");
        } catch (Exception e) {
            addErrorMessage(e.getMessage());
            LOGGER.error("Persistence exception: ", e);
        }
    }

    public List<Entity> getEntityList() {
        return entityList;
    }

    public void setEntityList(List<Entity> entityList) {
        this.entityList = entityList;
    }

    public Entity getCurrent() {
        return current;
    }

    public void setCurrent(Entity current) {
        this.current = current;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    protected abstract void validate() throws Exception;

    protected abstract AbstractFacade getAbstractFacade();

    protected abstract void initializeBean();

}
